﻿using SuperX.Common.Helper;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace SuperX.Common.User
{
    //
    public class UserService
    {
        public ConcurrentDictionary<string, Account> AccountDict = new ConcurrentDictionary<string, Account>();

        //
        public Account Account { get; set; }

        //
        private UserService()
        {
        }

        //
        private void Init()
        {
        }

        //
        public static UserService GetInstance()
        {
            if (_service == null)
            {
                lock (obj)
                {
                    if (_service == null)
                    {
                        _service = new UserService();
                    }
                }
            }
            return _service;
        }

        public Account GetAccount(string clientId)
        {
            AccountDict.TryGetValue(clientId, out Account account);
            return account;
        }

        public bool IsAdmin(string clientId)
        {
            var account = GetAccount(clientId);
            if (account == null) return false;
            return account.IsAdmin;
        }

        public void LoginUser(string clientId, Account account)
        {
            AccountDict.TryAdd(clientId, account);
        }

        public void LogoutUser(string clientId)
        {
            AccountDict.TryRemove(clientId, out Account account);
        }

        //
        private static UserService _service;

        //
        public static readonly object obj = new object();
    }
}